<?php

namespace app\http\middleware;

use other\Token;
use think\{
    Request as OriginRequest,facade\Validate
};

class IfLogin
{
    public function handle(OriginRequest $request, \Closure $next)
    {
        $data['token'] = session('token');
        $rules = [
            'token' => 'require'
        ];
        $v = Validate::make($rules, []);
        $result = $v->check($data);
        if ( ! $result ) {
            return redirect('/admin/login');
        }
        $info = Token::decode($data['token']);
        if ( $info['code'] !== 200 ) {
            return redirect('/admin/login');
        }
        $request->userInfo = (array)$info['data']['userInfo'];
        return $next($request);
    }
}
